package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.MD5Utils;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@BusinessDataConverter(
    auto = false,
    insertSql = "INSERT INTO ND_PROCESS_VARIABLE\n" +
        "(ID, PROCESS_ID, TASK_ID, NODE_KEY, NODE_VALUE)\n" +
        "VALUES(?,?,?,?,?)",
    updateSql = "UPDATE ND_PROCESS_VARIABLE\n" +
        "SET PROCESS_ID=?, TASK_ID=?, NODE_KEY=?, NODE_VALUE=?\n" +
        "WHERE ID = ?",
    findMigratedIdListSql = "SELECT ID FROM ND_PROCESS_VARIABLE \n" +
        "WHERE PROCESS_ID = ?"
)
public class NdProcessVariable extends AbstractWorkflowBusiness {
  public NdProcessVariable(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId);
  }

  @Override
  public void migrate() throws Exception {

  }


  public void customMigrate(String jbpmTaskInstanceId, Map<String,Object> taskVariableMap) throws SQLException {
    for(Map.Entry<String,Object> entry : taskVariableMap.entrySet()) {
      String targetId = MD5Utils.stringToMD5(this.procInstId + jbpmTaskInstanceId+ entry.getKey());
      int needMigratedResult = this.needMigrated(targetId);
      if(needMigratedResult == 0) {
        return;
      }
      List values = new ArrayList();
      values.add(targetId);
      values.add(this.procInstId+"-JW");
      values.add(jbpmTaskInstanceId+"-JW");
      values.add(entry.getKey());
      values.add(entry.getValue());
      if(needMigratedResult == 1) {
        this.targetDbUtil.updateByParams(updateSql,values,true);
      } else {
        this.targetDbUtil.updateByParams(insertSql,values);
      }
    }
  }

  public void migrateProcFormStr(String procFormStr) throws SQLException {
    List<Object> list = this.targetDbUtil.selectSingleColumn("SELECT ID FROM ND_PROCESS_VARIABLE WHERE NODE_KEY = 'procFormStr' AND PROCESS_ID = ? "
        , Arrays.asList(this.procInstId + "-JW"));
    String targetId = null;
    if(list.size() == 0) {
      targetId = MD5Utils.stringToMD5(this.procInstId + "procFormStr");
    } else {
      targetId = list.get(0).toString();
    }
    int needMigratedResult = this.needMigrated(targetId);
    if(needMigratedResult == 0) {
      return;
    }
    List values = new ArrayList();
    values.add(targetId);
    values.add(this.procInstId+"-JW");
    values.add(null);
    values.add("procFormStr");
    values.add(procFormStr);
    if(needMigratedResult == 1) {
      this.targetDbUtil.updateByParams(updateSql,values,true);
    } else {
      this.targetDbUtil.updateByParams(insertSql,values);
    }
  }
}
